{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Manipulate integral and floating point numbers in C#\n",
    "\n",
    "*In this tutorial about numeric types, you'll use Jupyter notebooks to learn C# interactively. You're going to write C# code and see the results of compiling and running your code directly in the notebook.*\n",
    "\n",
    "It contains a series of lessons that explore numbers and math operations in C#. These lessons teach you the fundamentals of the C# language.\n",
    "\n",
    "## Working with integer math\n",
    "\n",
    "Run the following cell:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "int a = 18;\n",
    "int b = 6;\n",
    "int c = a + b;\n",
    "Console.WriteLine(c);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You've seen one of the fundamental math operations with integers. The `int` type represents an **integer**, a positive or negative whole number. You use the `+` symbol for addition. Other common mathematical operations for integers include:\n",
    "\n",
    "- `-` for subtraction\n",
    "- `*` for multiplication\n",
    "- `/` for division\n",
    "\n",
    "Start by exploring those different operations. Modify the third line to try each of these operations. After each edit, select the **Run** button.\n",
    "\n",
    "- Subtraction: `int c = a - b;`\n",
    "- Multiplication: `int c = a * b;`\n",
    "- Division: `int c = a / b;`\n",
    "```\n",
    "\n",
    "You can also experiment by writing multiple mathematics operations in the same line, if you'd like.\n",
    "\n",
    "> As you explore C# (or any programming language), you'll make mistakes when you write code. The **compiler** will find those errors and report them to you. When the output contains error messages, look closely at the example code, and the code in the interactive window to see what to fix. That exercise will help you learn the structure of C# code.\n",
    "\n",
    "## Explore order of operations\n",
    "\n",
    "The C# language defines the precedence of different mathematics operations with rules consistent with the rules you learned in mathematics. Multiplication and division take precedence over addition and subtraction. Explore that by running the following cell:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "int a = 5;\n",
    "int b = 4;\n",
    "int c = 2;\n",
    "int d = a + b * c;\n",
    "Console.WriteLine(d);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The output demonstrates that the multiplication is performed before the addition.\n",
    "\n",
    "You can force a different order of operation by adding parentheses around the operation or operations you want performed first. Modify the fourth line in the cell above to force the addition to be performed first: `int d = (a + b) * c;`\n",
    "\n",
    "Explore more by combining many different operations. Replace the fourth line above with something like this. `int d = (a + b) - 6 * c + (12 * 4) / 3 + 12;`\n",
    "\n",
    "You may have noticed an interesting behavior for integers. Integer division always produces an integer result, even when you'd expect the result to include a decimal or fractional portion.\n",
    "\n",
    "## Explore integer precision and limits\n",
    "\n",
    "If you haven't seen this behavior, try the following cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "int a = 7;\n",
    "int b = 4;\n",
    "int c = 3;\n",
    "int d = (a + b) / c;\n",
    "int e = (a + b) % c;\n",
    "Console.WriteLine($\"quotient: {d}\");\n",
    "Console.WriteLine($\"remainder: {e}\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That last sample showed you that integer division truncates the result. It showed how you can get the **remainder** by using the **remainder** operator, the `%` character.\n",
    "\n",
    "The C# integer type differs from mathematical integers in one other way: the `int` type has minimum and maximum limits. Run this cell to see those limits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "int max = int.MaxValue;\n",
    "int min = int.MinValue;\n",
    "Console.WriteLine($\"The range of integers is {min} to {max}\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If a calculation produces a value that exceeds those limits, you have an **underflow** or **overflow** condition. The answer appears to wrap from one limit to the other. Add these two lines to the preceding cell to see an example:\n",
    "\n",
    "```csharp\n",
    "int what = max + 3;\n",
    "Console.WriteLine($\"An example of overflow: {what}\");\n",
    "```\n",
    "\n",
    "Notice that the answer is very close to the minimum (negative) integer. It's the same as `min + 2`. The addition operation **overflowed** the allowed values for integers. The answer is a very large negative number because an overflow \"wraps around\" from the largest possible integer value to the smallest.\n",
    "\n",
    "There are other numeric types with different limits and precision that you would use when the `int` type doesn't meet your needs. Let's explore those types of numbers next.\n",
    "\n",
    "## Work with the double type\n",
    "\n",
    "The `double` numeric type represents a double-precision floating point number. Those terms may be new to you. A **floating point** number is useful to represent non-integral numbers that may be very large or small in magnitude. **Double-precision** is a relative term that describes the numbers of binary digits used to store the value. **Double precision** number have twice the number of binary digits as **single-precision**. On modern computers, it is more common to use double precision than single precision numbers. **Single precision** numbers are declared using the `float` keyword. Let's explore. Try the following cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "double a = 5;\n",
    "double b = 4;\n",
    "double c = 2;\n",
    "double d = (a + b) / c;\n",
    "Console.WriteLine(d);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that the answer includes the decimal portion of the quotient. Try a slightly more complicated expression with doubles:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "double a = 19;\n",
    "double b = 23;\n",
    "double c = 8;\n",
    "double d = (a + b) / c;\n",
    "Console.WriteLine(d);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The range of a double value is much greater than integer values. Try the following cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "double max = double.MaxValue;\n",
    "double min = double.MinValue;\n",
    "Console.WriteLine($\"The range of double is {min} to {max}\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These values are printed out in scientific notation. The number to the left of the `E` is the significand. The number to the right is the exponent,\n",
    "as a power of 10.\n",
    "    \n",
    "Just like decimal numbers in math, doubles in C# can have rounding errors. Try this cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "double third = 1.0 / 3.0;\n",
    "Console.WriteLine(third);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You know that `0.3` is `3/10` and not exactly the same as `1/3`. Similarly, `0.33` is `33/100`. That's closer to `1/3`, but still not exact. \n",
    "\n",
    "***Challenge***\n",
    "\n",
    "Try other calculations with large numbers, small numbers, multiplication, and division using the `double` type.  Try more complicated calculations. Use the cell below for your ideas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Work with decimal types\n",
    "\n",
    "You've seen the basic numeric types in C#: integers and doubles.  There's one other type to learn: the `decimal` type. The `decimal` type has a smaller range but greater precision than `double`. Let's take a look:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "decimal min = decimal.MinValue;\n",
    "decimal max = decimal.MaxValue;\n",
    "Console.WriteLine($\"The range of the decimal type is {min} to {max}\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that the range is smaller than the `double` type. You can see the greater precision with the decimal type by trying the following cell:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "double a = 1.0;\n",
    "double b = 3.0;\n",
    "Console.WriteLine(a / b);\n",
    "\n",
    "decimal c = 1.0M;\n",
    "decimal d = 3.0M;\n",
    "Console.WriteLine(c / d);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `M` suffix on the numbers is how you indicate that a constant should use the `decimal` type.\n",
    "\n",
    "Notice that the math using the decimal type has more digits to the right of the decimal point.\n",
    "\n",
    "The `M` suffix on the numbers is how you indicate that a constant should use the `decimal` type. Otherwise, the compiler assumes the `double` type.\n",
    "\n",
    "> The letter `M` was chosen as the most visually distinct letter between the `double` and `decimal` keywords.\n",
    "\n",
    "Notice that the math using the decimal type has more digits to the right of the decimal point.\n",
    "\n",
    "***Challenge***\n",
    "\n",
    "Now that you've seen the different numeric types, write code that calculates the area of a circle whose radius is 2.50 centimeters. Remember that the area of a circle is the radius squared multiplied by PI. One hint: .NET contains a constant for PI, `System.Math.PI` that you can use for that value. `System.Math.PI`, like all constants declared in the `System.Math` namespace, is a `double` value. For that reason, you should use `double` instead of `decimal` values for this challenge.\n",
    "\n",
    "You should get an answer between 19 and 20."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, try the tutorial on [branches and loops](branches-and-loops.ipynb)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "C#",
   "pygments_lexer": "csharp",
   "version": "8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
